package com.example.tonjie.software_park_association;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;

public class Small_class extends AppCompatActivity implements View.OnClickListener {

    private VideoView videoView;
    private ImageView button_play, button_pause, button_stop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_small_class);
        init();//控件id的获取
        jurisdiction();//获取权限
    }

    /**
     * 获取权限
     */
    private void jurisdiction() {
        if(ContextCompat.checkSelfPermission(Small_class.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(Small_class.this,new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            },1);
        }else{
            initVideeoPath();
        }
    }

    /**
     * 控件id的获取
     */
    private void init() {
        videoView = (VideoView) findViewById(R.id.videoView);
        button_play = (ImageView) findViewById(R.id.button_play);
        button_pause = (ImageView) findViewById(R.id.button_pause);
        button_stop = (ImageView) findViewById(R.id.button_stop);
        button_stop.setOnClickListener(this);
        button_pause.setOnClickListener(this);
        button_play.setOnClickListener(this);
    }

    /**
     * 制定视频文件的路径
     */
    private void initVideeoPath() {
        File file=new File(Environment.getExternalStorageDirectory(),"a.mp4");
        videoView.setVideoPath(file.getPath());
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1:
                if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    initVideeoPath();
                }else{
                    Toast.makeText(this, "拒绝权限将无法使用程序", Toast.LENGTH_SHORT).show();
                }
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.button_play:
                if(!videoView.isPlaying()){
                    videoView.start();
                }
                break;
            case R.id.button_stop:
                if(videoView.isPlaying()){
                    videoView.resume();
                }
                break;
            case R.id.button_pause:
                if(videoView.isPlaying()){
                    videoView.pause();
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(videoView!=null){
            videoView.suspend();
        }
    }
}
